import 'package:meta/meta.dart';

/// API 基础响应模型
class BaseResponse<T> {
  /// 响应码
  final int code;
  
  /// 响应消息
  final String message;
  
  /// 数据
  final T? data;
  
  /// 是否成功
  bool get isSuccess => code == 200 || code == 0;
  
  BaseResponse({
    required this.code,
    required this.message,
    this.data,
  });
  
  /// 从JSON映射创建BaseResponse
  factory BaseResponse.fromJson(Map<String, dynamic> json, T Function(Object? json)? fromJsonT) {
    return BaseResponse<T>(
      code: json['code'] as int? ?? -1,
      message: json['message'] as String? ?? 'Unknown error',
      data: json['data'] != null && fromJsonT != null ? fromJsonT(json['data']) : null,
    );
  }
  
  /// 创建空响应
  factory BaseResponse.empty() {
    return BaseResponse<T>(code: -1, message: 'Unknown error', data: null);
  }
  
  /// 创建错误响应
  factory BaseResponse.error(String code, String message) {
    return BaseResponse<T>(code: int.parse(code), message: message, data: null);
  }
  
  /// 创建成功响应
  factory BaseResponse.success(T? data, [String message = '操作成功']) {
    return BaseResponse<T>(code: 200, message: message, data: data);
  }
  
  /// 转换为JSON
  Map<String, dynamic> toJson(Map<String, dynamic> Function(T)? toJsonT) {
    final Map<String, dynamic> json = {
      'code': code,
      'message': message,
    };
    
    if (data != null && toJsonT != null) {
      json['data'] = toJsonT(data as T);
    } else if (data != null) {
      json['data'] = data;
    }
    
    return json;
  }
  
  /// 转换数据类型
  BaseResponse<R> copyWith<R>({
    int? code,
    String? message,
    R? data,
  }) {
    return BaseResponse<R>(
      code: code ?? this.code,
      message: message ?? this.message,
      data: data,
    );
  }
  
  /// 将列表数据转换为BaseResponse
  static BaseResponse<List<T>> fromJsonList<T>(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJsonT,
  ) {
    final List<dynamic>? dataList = json['data'] as List<dynamic>?;
    
    return BaseResponse<List<T>>(
      code: json['code'] as int? ?? -1,
      message: json['message']?.toString() ?? '',
      data: dataList?.map((item) => fromJsonT(item as Map<String, dynamic>)).toList(),
    );
  }
  
  /// 将分页数据转换为BaseResponse
  static BaseResponse<Pagination<T>> fromJsonPagination<T>(
    Map<String, dynamic> json,
    T Function(Map<String, dynamic>) fromJsonT,
  ) {
    final Map<String, dynamic>? paginationData = json['data'] as Map<String, dynamic>?;
    
    if (paginationData == null) {
      return BaseResponse<Pagination<T>>(
        code: json['code'] as int? ?? -1,
        message: json['message']?.toString() ?? '',
        data: null,
      );
    }
    
    final List<dynamic>? items = paginationData['items'] as List<dynamic>?;
    
    return BaseResponse<Pagination<T>>(
      code: json['code'] as int? ?? -1,
      message: json['message']?.toString() ?? '',
      data: Pagination<T>(
        current: paginationData['current'] as int? ?? 1,
        pageSize: paginationData['pageSize'] as int? ?? 10,
        total: paginationData['total'] as int? ?? 0,
        items: items?.map((item) => fromJsonT(item as Map<String, dynamic>)).toList() ?? [],
      ),
    );
  }

  @override
  String toString() {
    return 'BaseResponse(code: $code, message: $message, data: $data)';
  }
}

/// 分页数据模型
class Pagination<T> {
  /// 当前页
  final int current;
  
  /// 每页大小
  final int pageSize;
  
  /// 总数
  final int total;
  
  /// 数据项
  final List<T> items;
  
  /// 总页数
  int get totalPages => (total / pageSize).ceil();
  
  /// 是否为第一页
  bool get isFirstPage => current <= 1;
  
  /// 是否为最后一页
  bool get isLastPage => current >= totalPages;
  
  Pagination({
    required this.current,
    required this.pageSize,
    required this.total,
    required this.items,
  });
  
  /// 转换为Map
  Map<String, dynamic> toJson(Map<String, dynamic> Function(T) toJsonT) {
    return {
      'current': current,
      'pageSize': pageSize,
      'total': total,
      'items': items.map((item) => toJsonT(item)).toList(),
    };
  }
} 